URL = "http://svn.apache.org/repos/asf/xalan/c/trunk"

# There is no official 1.11 release (yet) but is often referred as such on the internet
# Note that the last official release 1.10 does not compile with Xerces 3.1.1.
VERSIONS = ["1_11_0"]
DEFAULT_VERSION = "1_11_0"
DEPENDS = ["xercesc"]

from source.builders import ConfigureMakeBuilder, VisualStudioBuilder, FetchSourceSVN
import os

class FetchSourceXalanc(FetchSourceSVN):

  def fetch(self, url, downloadlocation, buildlocation ):
    targetdir = "xalanc-%s" % DEFAULT_VERSION #todo allow custom version
    targetpath = buildlocation + os.sep  + targetdir
    if os.path.exists( targetpath ):
      os.system( self.executable + " update " + targetpath )
    else:
      os.system( self.executable + " checkout " + url + " " + targetpath )
    return targetpath

class XalancConfigureBuilder( ConfigureMakeBuilder ):

  def downloadextract(self):
    return FetchSourceXalanc()

  def targetpath(self):
    return self.runtime.config.directories.enviro + os.sep + "xalanc-" + self.version

  def configureparameters( self ):
    return [ "-p linux",
             "-c gcc",
             "-x g++",
             "-P " + self.targetpath() ]

  def configure( self ):
    os.chdir( self.extractedpath )
    xercescdir = self.dependencies["xercesc"].targetpath()
    os.environ['XERCESCROOT'] = xercescdir
    os.environ['XALANCROOT'] = self.extractedpath

    self.execute( "./runConfigure " + " ".join( self.configureparameters() ) )

  def make( self ):
    os.chdir( self.extractedpath )
    xercescdir = self.dependencies["xercesc"].targetpath()
    os.environ['XERCESCROOT'] = xercescdir
    os.environ['XALANCROOT'] = self.extractedpath

    self.write( "XERCESCROOT = " + xercescdir )

    if self.machine.cores > 1:
      self.execute( "make -j " + str(self.machine.cores) )
    else:
      self.execute( "make" )

  def install( self ):
    os.chdir( self.extractedpath )
    self.execute( "make install" )

class XalancWindowsBuilder( VisualStudioBuilder ):

  def downloadextract(self):
    return FetchSourceXalanc()

  def targetpath(self):
    return self.runtime.config.directories.enviro + os.sep + "xalanc-" + self.version

  def make(self):
    os.chdir( self.extractedpath )

    # Set up required environment variables
    xercescdir = self.dependencies["xercesc"].targetpath()
    os.environ['XERCESCROOT'] = xercescdir
    # The Xerces is used to generate the ICU message files so this is necessary
    os.environ['PATH'] = os.environ['PATH'] + ";" + xercescdir + "/bin"

    # Buildext supports VC9
    slndir = "Projects/Win32/VC9"
    slnfile = "Xalan.sln"
    os.chdir( slndir )

    # Convert to 64 bits if necessary
    replacements = []
    if self.machine.bits == 64:
      self.convertSolutionTo64(slnfile)      
      replacements += self.getx64replacements()
      replacements.append(( "^				TargetMachine=\"1\"", "TargetMachine=\"17\""))
    self.replaceLinesInFile( slnfile, replacements )

    debug = {0: "Release", 1: "Debug"}[self.debug]
    self.execute( "devenv.com Xalan.sln /Build " + debug )

  def install(self):
    os.chdir( self.extractedpath )
    if not os.path.exists( self.targetpath()+"/include" ):
      self.copydir( "src/xalanc", self.targetpath()+"/include/xalanc" )

    os.chdir( self.extractedpath + "/Build/Win32/VC9" )
    config = {0: "Release", 1: "Debug"}[self.debug]
    self.copypattern( config, "(.*).exe", self.targetpath()+"/bin" )
    self.copypattern( config, "(.*).dll", self.targetpath()+"/bin" )
    self.copypattern( config, "(.*).lib", self.targetpath()+"/lib" )

def decider( machine, dirs ):
  return { "Linux" : XalancConfigureBuilder,
           "Windows" : XalancWindowsBuilder }[machine.platform]

BUILDER = decider
